import { login } from '@/api/login.js'
import { AUTHORIZATION } from '@/api/config.js'
const autoLogin = {
	methods: {
		uniLogin() {
			return new Promise(resolve => {
				uni.login({
					success: (loginRes) => {
						login({
							code: loginRes.code,
						}).then(res => {
							console.log('登录成功')
							if (res.code === 1) {
								const data = res.data
								// token
								const token = data.token
								this.$cache.put(AUTHORIZATION, token.accessToken);
								// 用户信息
								const userInfo = data.userInfo
								this.$cache.put(this.$cache.key.userInfo, userInfo)
								// 页面信息
								const pages = data.pages
								if (pages) {
									this.$cache.put(this.$cache.key.pages, pages)
								}
								resolve(true)
							} else {
								console.log('登录失败！')
								resolve(false)
							}
						})
					},
					fail: (e) => {
						console.log('微信登录失败！')
						resolve(false)
					}
				})
			})
		}
	}
}
export default autoLogin;